package com.nhim.cms.util;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.nhim.cms.dao.ElementTemplateDAO;
import com.nhim.cms.domain.ElementTemplate;

@Service
public class ElementTemplateManager {
	@Autowired
	private ElementTemplateDAO elementTemplateDAO;

	@PostConstruct
	public void init() throws Exception {
	}

	@PreDestroy
	public void destroy() {
	}
	@Transactional(readOnly = true)
	public ElementTemplate findById(Integer id) {

		return elementTemplateDAO.findById(id);

	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void save(ElementTemplate entity) throws Exception {
		elementTemplateDAO.save(entity);
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public Boolean delete(Integer id) throws Exception {

		ElementTemplate elementTemplate = elementTemplateDAO.findById(id);

		if (elementTemplate != null)
		{
			elementTemplateDAO.delete(elementTemplate);
			return true;
		}
		return false;

	}
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void delete(ElementTemplate template)
	{
		elementTemplateDAO.delete(template);
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public ElementTemplate saveOrUpdate(ElementTemplate entity) throws Exception {

		return elementTemplateDAO.saveOrUpdate(entity);

	}
	@Transactional(readOnly = true)
	public List<ElementTemplate> findByElementDefinitionId(Integer id)
	{
		return elementTemplateDAO.findByElementDefinitionId(id);
	}
	@Transactional(readOnly = true)
	public ElementTemplate findByNameAndElementDefinitionId(Integer id, String name)
	{
		return elementTemplateDAO.findByNameAndElementDefinitionId(id, name);
	}

	public static ElementTemplateManager getFromApplicationContext(
			ApplicationContext ctx) {
		return (ElementTemplateManager) ctx
				.getBean(ElementTemplateManager.class);
	}
}
